import React,{useEffect} from "react";
import "./add.less";
import { Button, Checkbox, Form, Input, Select } from "antd";
import Title from "../../components/Title/Title";
import { addUser } from "../../api/account";
import { useNavigate } from 'react-router-dom';

const SubmitButton = ({ form, children }) => {
  const [submittable, setSubmittable] = React.useState(false);

  // Watch all values
  const values = Form.useWatch([], form);
  React.useEffect(() => {
      form
          .validateFields({
              validateOnly: true,
          })
          .then(() => setSubmittable(true))
          .catch(() => setSubmittable(false));
  }, [form, values]);
  return (
      <Button type="primary" htmlType="submit" disabled={!submittable}>
          {children}
      </Button>
  );
};
export default function Add() {
  const [form] = Form.useForm();
  const navigate = useNavigate()
  const onFinish = async (values) => {
      let res = await addUser(values);
      console.log(res.data);
      navigate('/account')
  };
  return (
    <>
      <div className="include">
        <div className="add">
        <Title title = '添加账号'></Title>
        </div>
        <Form
        
          labelCol={{
            span: 4,
          }}
          wrapperCol={{
            span: 24,
          }}
          style={{
            maxWidth: 400,
          }}
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
        >
          <Form.Item
            label="账号"
            name="account"
            rules={[
              {
                required: true,
                message: "Please input your username!",
              },
            ]}
          >
            <Input placeholder="请输入账号" />
          </Form.Item>

          <Form.Item
          name="password"
            label="密码"
            rules={[
              {
                required: true,
                message: "Please input your password!",
              },
            ]}
          >
            <Input.Password placeholder="请输入密码" />
          </Form.Item>
          <Form.Item label="用户组"  name="userGroup">
            <Select>
              <Select.Option value="超级管理员">超级管理员</Select.Option>
              <Select.Option value="普通管理员">普通管理员</Select.Option>
            </Select>
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <SubmitButton form={form} type="primary"  className="modify">
              立即添加
            </SubmitButton>
            <Button type="default" htmlType="reset" className="modify1">
              重置
            </Button>
          </Form.Item>
        </Form>
        ;
      </div>
    </>
  );
}
